package com.situ.day250116;

import org.junit.Test;

public class HomeWork {
    public static void main(String[] args) {
        /*String str = "I Love Java!";
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] >= 'A' && chars[i] <= 'Z') {
                chars[i] = (char) (chars[i] - 'A' + 'a');
            }
        }
        String result = new String(chars);
        System.out.println(result);*/
        //大写字母A到Z的值比对应的小写字母a到z的值小 32
        String str = "I Love Java!";
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] >= 'A' && charArray[i] <= 'Z') {
                charArray[i] = (char) (charArray[i] + 32);
            }
        }
        String result = new String(charArray);
        System.out.println(result);
    }

    @Test
    public void test(){
        String str = "   Hello, Java!   ";
        String newStr = trim(str);
        System.out.println(str.length());//18
        System.out.println("Newstr String: '" + newStr + "'");
    }

    public static String trim(String str) {
        //"" null 卫操作
        if (str == null || str.equals("")) {
            return "";
        }
        int start = 0;
        int end = str.length() - 1;

        // 找到第一个非空格字符的位置
        while (start <= end && str.charAt(start) == ' ') {
            start++;
        }
        System.out.println("start的值: " + start);//看最终start是几

        // 找到最后一个非空格字符的位置
        while (end >= start && str.charAt(end) == ' ') {
            end--;
        }
        System.out.println("end的值: " + end);

        // 截取并返回去除两端空格后的字符串
        return str.substring(start, end + 1);
    }
}
